package android.example.graphictest.fillrate;

import java.util.Random;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.view.View;

public class FillRateView extends View implements Runnable
{
    Paint mPaint                 = null;
    public static int h          = 0;
    public static int w          = 0;
    public static int h_nmb      = 0;
    public static int w_nmb      = 0;
    public static int frame      = 0;
    public static int count      = 0;
    public static long startTime = 0;
    public static long stopTime  = 0;
    public static float fps      = 0;
    public static float totalfps = 0;
    public static final int[] valueOfColor = {
  	  Color.BLUE, Color.RED, Color.CYAN, 
  	  Color.MAGENTA, Color.GREEN, Color.WHITE, 
        Color.LTGRAY, Color.YELLOW, Color.GRAY };
	public FillRateView(Context context)
   {
	   super(context);
	   mPaint = new Paint();
	   h      = FillRateActivity.screenHeight;
	   h_nmb  = (int)h/20;
	   w      = FillRateActivity.screenWidth;
	   w_nmb  = (int)w/20;
	   new Thread(this).start();
   }
   public void run()
   {
	   while(!Thread.currentThread().isInterrupted())
	   {
		   try
		   {
			   Thread.sleep(10);
		   }
		   catch(InterruptedException e)
		   {
			   Thread.currentThread().interrupt();
		   }
		   postInvalidate();
	   }
   }
   public void onDraw(Canvas canvas)
   {
	   super.onDraw(canvas);
	   mPaint.setStyle(Paint.Style.FILL);
	   if (startTime == 0)
	   {
		   startTime = System.currentTimeMillis();
	   }
	   for(int i = 0; i < h_nmb; i++)
	   {
		   for(int j = 0; j < w_nmb; j++)
		   {
			   int l = (int) (Math.random() * w);
			   int t = (int) (Math.random()* h);
			   int w_f = (int) (Math.random()*w);
			   int h_f = (int) (Math.random()*h);
			   mPaint.setColor(valueOfColor[(int)(10*Math.random())%9]);
			   //canvas.drawRect(j*20, i*20, (j+1)*20, (i+1)*20, mPaint);
			   canvas.drawRect(l, t, w_f, h_f, mPaint);
		   }
	   }
	   stopTime = System.currentTimeMillis();
	   frame++;
	   if(frame > 50)
	   {
		   fps = (float)(10000*frame)/(float)(stopTime - startTime - frame*10);
		   startTime = 0;
		   frame = 0;
		   count++;
		   if(count > 1 && count < 7)
		   {
		     totalfps += fps;
		   }
	   }
	   
	   
   }
}
